<?php

/**
 * This is the model class for table "advertising".
 *
 * The followings are the available columns in table 'advertising':
 * @property integer $advertising_id
 * @property integer $category_id
 * @property string $title
 * @property string $description
 * @property integer $type_id
 * @property string $manufacturer
 * @property double $price
 * @property string $image
 * @property string $author
 * @property string $email
 * @property string $country
 * @property string $state
 * @property string $city
 * @property integer $created
 * @property integer $updated
 * @property integer $expired
 * @property integer $featured
 * @property integer $created_by
 * @property integer $updated_by
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property Categories $category
 * @property Types $type
 */
class Advertising extends CActiveRecord {

    const STATUS_DRAFT = 1;
    const STATUS_PUBLISHED = 2;
    const STATUS_ARCHIVED = 3;

    public $expireDate = '';

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Advertising the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{advertising}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('category_id, title, description, author, email, status', 'required'),
            array('category_id, type_id, created, updated, expired, featured, created_by, updated_by, status', 'numerical', 'integerOnly' => true),
            array('price', 'numerical'),
            array('title, manufacturer, image', 'length', 'max' => 255),
            array('author, email, country, state, city', 'length', 'max' => 60),
            array('expireDate', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('advertising_id, category_id, title, description, type_id, manufacturer, price, image, author, email, country, state, city, created, updated, expired, featured, created_by, updated_by, status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
            'type' => array(self::BELONGS_TO, 'Type', 'type_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'advertising_id' => 'Advertising',
            'category_id' => 'Category',
            'title' => 'Title',
            'description' => 'Description',
            'type_id' => 'Type',
            'manufacturer' => 'Manufacturer',
            'price' => 'Price',
            'image' => 'Image',
            'author' => 'Author',
            'email' => 'Email',
            'country' => 'Country',
            'state' => 'State',
            'city' => 'City',
            'created' => 'Created',
            'updated' => 'Updated',
            'expired' => 'Expired',
            'featured' => 'Featured',
            'created_by' => 'Created By',
            'updated_by' => 'Updated By',
            'status' => 'Status',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('advertising_id', $this->advertising_id);
        $criteria->compare('category_id', $this->category_id);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('type_id', $this->type_id);
        $criteria->compare('manufacturer', $this->manufacturer, true);
        $criteria->compare('price', $this->price);
        $criteria->compare('image', $this->image, true);
        $criteria->compare('author', $this->author, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('country', $this->country, true);
        $criteria->compare('state', $this->state, true);
        $criteria->compare('city', $this->city, true);
        $criteria->compare('created', $this->created);
        $criteria->compare('updated', $this->updated);
        $criteria->compare('expired', $this->expired);
        $criteria->compare('featured', $this->featured);
        $criteria->compare('created_by', $this->created_by);
        $criteria->compare('updated_by', $this->updated_by);
        $criteria->compare('status', $this->status);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * get array status support
     * @return array
     */
    public static function getStatusArray(){
        return array(
            self::STATUS_PUBLISHED => 'Publish',
            self::STATUS_DRAFT => 'Draft',
            self::STATUS_ARCHIVED => 'Archive'
        );
    }

    /**
     * add exp time
     * @return type
     */
    public static function getExpireTime(){
        return array(
            604800 => '1 week',
            1209600 => '2 weeks',
            2592000 => '1 month'
        );
    }

    /**
     * action before save
     * @return boolean
     */
    public function beforeSave() {
        if(parent::beforeSave()){
            if($this->isNewRecord){
                $this->created = $this->updated = time();

                if(!user()->isGuest){
                    $this->created_by = user()->id;
                }
            }else{
                $this->updated_by = user()->id;
            }


            $this->updated = time();

            return true;
        }
    }

    /**
     *
     * @return string
     */
    public function getImageUrl($thumb = true){
        if($this->image != ''){
            if($thumb){
                return ImageHelper::thumb(120, 150, $this->image);
            }else{
                return UPLOAD_URL . $this->image;
            }
        }

        //return default image
        return '';
    }

    /**
     * get the front end url
     * @return string
     */
    public function getUrl(){
        return url('/advertising/view', array(
            'id' => $this->advertising_id,
            'name' => $this->title
        ));
    }

    public function afterFind() {
        parent::afterFind();

        //bind exp date
        $this->expireDate = date('m/d/Y', $this->expired);
    }

    public function getFormatId(){
        return str_pad($this->advertising_id, 11);
    }

    /**
     * Format price by currency
     *
     */
    public function getFormatPrice(){
        if($this->price == ''){
            return 'All reasonable offers considered.';
        }

        return '$'.number_format($this->price, 2);
    }
}